﻿using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Converters
{
    /// <summary>
    /// GridColumns转换
    /// </summary>
    public class XGridColumnsConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            List<GridColumn> columns = new List<GridColumn>();
            if (valueNode.ValueNodes != null)
            {
                foreach (Metadata.ValueNode vN in valueNode.ValueNodes)
                {
                    XGridColumnConverter gdColumnConvert = new XGridColumnConverter();
                    columns.Add((GridColumn)gdColumnConvert.Convert(context, vN));
                }
            }
            return columns;
        }
        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            List<GridColumn> gridColumns = (List<GridColumn>)value;
            Metadata.ValueNode valueNode = new Metadata.ValueNode("GridColumns");
            valueNode.ValueNodes = new List<Metadata.ValueNode>();
            valueNode.Attributes = new Dictionary<string, string>();

            XGridColumnConverter xGridColumnConverter = new XGridColumnConverter();
            foreach (GridColumn gc in gridColumns)
            {
                valueNode.ValueNodes.Add(xGridColumnConverter.ConvertBack(context, gc));
            }
            return valueNode;
        }
    }

    public class XGridColumnConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            GridColumn column = new GridColumn();
            string name, caption, dataItemId, width, hAlignment, format;
            string valueType = string.Empty;
            string padding = string.Empty;
            valueNode.Attributes.TryGetValue("Name", out name);
            valueNode.Attributes.TryGetValue("Caption", out caption);
            valueNode.Attributes.TryGetValue("DataItemId", out dataItemId);
            valueNode.Attributes.TryGetValue("Width", out width);
            valueNode.Attributes.TryGetValue("HAlignment", out hAlignment);
            valueNode.Attributes.TryGetValue("TextFormat", out format);
            valueNode.Attributes.TryGetValue("ValueType", out valueType);
            valueNode.Attributes.TryGetValue("Padding", out padding);

            column.Name = name;
            column.Caption = caption;
            column.DataItemId = dataItemId;
            column.Width = string.IsNullOrEmpty(width) ? (int?)null : System.Convert.ToInt32(width);
            column.HAlignment = string.IsNullOrEmpty(hAlignment) ? "Center" : hAlignment;
            column.TextFormat = format;
            column.ValueType = valueType;

            if (!string.IsNullOrEmpty(padding))
            {
                string[] paddingArr = padding.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (paddingArr.Length == 4)
                {
                    column.Padding = new System.Windows.Thickness(CPPEI.Coolzon.Common.Convertor.ToDouble(paddingArr[0]),
                        CPPEI.Coolzon.Common.Convertor.ToDouble(paddingArr[1]),
                        CPPEI.Coolzon.Common.Convertor.ToDouble(paddingArr[2]),
                        CPPEI.Coolzon.Common.Convertor.ToDouble(paddingArr[3]));
                }
            }

            if (valueNode.ValueNodes != null && valueNode.ValueNodes.Any())
            {
                column.Columns = new List<GridColumn>();
                valueNode.ValueNodes.ForEach((a) =>
                {
                    if (a.Name.Equals("GridColumn.Prefix"))
                    {
                        XGridColumnConditionImageConverter ccic = new XGridColumnConditionImageConverter();
                        column.ImagePrefixs = (List<GridColumnConditionImage>)ccic.Convert(context, a);
                    }
                    else if (a.Name.Equals("GridColumn.Suffix"))
                    {
                        XGridColumnConditionImageConverter ccic = new XGridColumnConditionImageConverter();
                        column.ImageSuffixs = (List<GridColumnConditionImage>)ccic.Convert(context, a);
                    }
                    else if (a.Name.Equals("GridColumn"))
                    {
                        column.Columns.Add((GridColumn)Convert(context, a));
                    }
                });
            }
            return column;
        }
        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            GridColumn gridColumn = (GridColumn)value;
            Metadata.ValueNode valueNode = new Metadata.ValueNode("GridColumn");
            valueNode.ValueNodes = new List<Metadata.ValueNode>();
            valueNode.Attributes = new Dictionary<string, string>();
            if (gridColumn == null)
            {
                return valueNode;
            }
            if (!string.IsNullOrWhiteSpace(gridColumn.Name))
                valueNode.Attributes.Add("Name", gridColumn.Name);
            if (!string.IsNullOrWhiteSpace(gridColumn.Caption))
                valueNode.Attributes.Add("Caption", gridColumn.Caption);
            if (!string.IsNullOrWhiteSpace(gridColumn.DataItemId))
                valueNode.Attributes.Add("DataItemId", gridColumn.DataItemId);
            if (!string.IsNullOrWhiteSpace(gridColumn.HAlignment))
                valueNode.Attributes.Add("HAlignment", gridColumn.HAlignment);
            if (!string.IsNullOrWhiteSpace(gridColumn.ValueType))
                valueNode.Attributes.Add("ValueType", gridColumn.ValueType);
            if (!string.IsNullOrWhiteSpace(gridColumn.VAlignment))
                valueNode.Attributes.Add("VAlignment", gridColumn.VAlignment);
            if (!string.IsNullOrWhiteSpace(gridColumn.TextFormat))
                valueNode.Attributes.Add("TextFormat", gridColumn.TextFormat);
            if (gridColumn.Width != null && gridColumn.Width.HasValue)
                valueNode.Attributes.Add("Width", gridColumn.Width.Value.ToString());

            valueNode.Attributes.Add("Padding", string.Format("{0},{1},{2},{3}", gridColumn.Padding.Left, gridColumn.Padding.Top, gridColumn.Padding.Right, gridColumn.Padding.Bottom));

            if (gridColumn.Columns != null && gridColumn.Columns.Count > 0)
            {
                XGridColumnConverter xGridColumnConverter = new XGridColumnConverter();
                foreach (var column in gridColumn.Columns)
                {
                    Metadata.ValueNode vN = new Metadata.ValueNode("GridColumn");
                    vN.ValueNodes = new List<Metadata.ValueNode>();
                    vN.ValueNodes.Add(xGridColumnConverter.ConvertBack(context, column));
                    valueNode.ValueNodes.Add(vN);
                }
            }
            XGridColumnConditionImageConverter xConditionImageConverter = new XGridColumnConditionImageConverter();
            if (gridColumn.ImagePrefixs != null && gridColumn.ImagePrefixs.Count > 0)
            {
                Metadata.ValueNode vN = new Metadata.ValueNode("GridColumn.Prefix");
                vN.ValueNodes = new List<Metadata.ValueNode>();
                foreach (var prefix in gridColumn.ImagePrefixs)
                {
                    vN.ValueNodes.Add(xConditionImageConverter.ConvertBack(context, prefix));
                }
                valueNode.ValueNodes.Add(vN);
            }
            if (gridColumn.ImageSuffixs != null && gridColumn.ImageSuffixs.Count > 0)
            {
                Metadata.ValueNode vN = new Metadata.ValueNode("GridColumn.Suffix");
                vN.ValueNodes = new List<Metadata.ValueNode>();
                foreach (var suffix in gridColumn.ImageSuffixs)
                {
                    vN.ValueNodes.Add(xConditionImageConverter.ConvertBack(context, suffix));
                }
                valueNode.ValueNodes.Add(vN);
            }
            return valueNode;
        }
    }

    public class XGridColumnConditionImageConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            List<GridColumnConditionImage> columns = new List<GridColumnConditionImage>();

            foreach (Metadata.ValueNode vN in valueNode.ValueNodes)
            {
                if (vN.Name.Equals("ConditionImage") && vN.ValueNodes != null && vN.ValueNodes.Any())
                {
                    GridColumnConditionImage column = new GridColumnConditionImage();
                    foreach (Metadata.ValueNode conditionImageNode in vN.ValueNodes)
                    {

                        if (conditionImageNode.Name.Equals("ConditionImage.ImageSource"))
                        {
                            Metadata.ValueNode imageNode = conditionImageNode.ValueNodes.Where(p => p.Name.Equals("ImageSource")).First();
                            XImageSourceFileConverter xImageSourceConverter = new XImageSourceFileConverter();
                            column.ImageSource = (ImageSourceFile)xImageSourceConverter.Convert(context, imageNode);
                        }
                        else if (conditionImageNode.Name.Equals("ConditionImage.Condtion"))
                        {
                            column.Condition = conditionImageNode.Value;
                        }
                        else if (conditionImageNode.Name.Equals("ConditionImage.Template"))
                        {
                            Metadata.ValueNode partNode = conditionImageNode.ValueNodes.Where(p => p.Name.Equals("Template")).First();
                            column.TemplateName = partNode.Attributes["Name"];
                        }
                    }
                    columns.Add(column);
                }
            }
            return columns;
        }
        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            Metadata.ValueNode valueNode = new Metadata.ValueNode("ConditionImage");
            valueNode.ValueNodes = new List<Metadata.ValueNode>();
            valueNode.Attributes = new Dictionary<string, string>();

            XImageSourceFileConverter xImageSourceConverter = new XImageSourceFileConverter();
            GridColumnConditionImage column = (GridColumnConditionImage)value;
            if (column != null)
            {
                Metadata.ValueNode vImageSource = new Metadata.ValueNode("ConditionImage.ImageSource");
                vImageSource.ValueNodes = new List<Metadata.ValueNode>();
                if(column.ImageSource != null)
                {
                    vImageSource.ValueNodes.Add(xImageSourceConverter.ConvertBack(context, column.ImageSource));
                    valueNode.ValueNodes.Add(vImageSource);
                }

                if (!string.IsNullOrWhiteSpace(column.Condition))
                {
                    Metadata.ValueNode vCondtion = new Metadata.ValueNode("ConditionImage.Condtion");
                    vCondtion.Value = column.Condition;
                    valueNode.ValueNodes.Add(vCondtion);
                }

                if (!string.IsNullOrWhiteSpace(column.TemplateName))
                {
                    Metadata.ValueNode vTemplate = new Metadata.ValueNode("ConditionImage.Template");
                    vTemplate.ValueNodes = new List<Metadata.ValueNode>();
                    Metadata.ValueNode vTem = new Metadata.ValueNode("Template");
                    vTem.Attributes = new Dictionary<string, string>();
                    vTem.Attributes.Add("Name", column.TemplateName);
                    vTemplate.ValueNodes.Add(vTem);
                    valueNode.ValueNodes.Add(vTemplate);
                }
            }
            return valueNode;
        }
    }
}
